package com.hzt.system.controller;

import com.hzt.system.common.ActiveUser;
import com.hzt.system.common.ResultObj;
import com.hzt.system.domain.Notice;
import com.hzt.system.service.NoticeService;
import com.hzt.system.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*
*  公告信息
* @Author:hzt
* @Date:11:10 下午 2020/7/12
*/
@RequestMapping("api/notice")
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("loadAllNotice")
    public Object loadAllNotice(NoticeVo noticeVo){
        return this.noticeService.loadAllNotice(noticeVo);
    }


    @RequestMapping("addNotice")
    public ResultObj addNotice(Notice notice){

        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser principal = (ActiveUser) subject.getPrincipal();
            String name = principal.getUser().getName();
            notice.setCreatetime(new Date());
            notice.setOpername(name);
            noticeService.save(notice);
            return  ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultObj.ADD_SUCCESS;
        }
    }


    @RequestMapping("updateNotice")
    public ResultObj updateNotice(Notice notice){
        try {
            noticeService.updateById(notice);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteNotice")
    public ResultObj deleteNotice(Notice notice){
        try {
            noticeService.removeById(notice.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除
    @RequestMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(Integer[] ids){
        try {
            List<Integer> listIds=new ArrayList<>();
            for (Integer id : ids) {
                listIds.add(id);
            }
            noticeService.removeByIds(listIds);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
